﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Anay.TaskScheduling.Common;
using Anay.TaskScheduling.JobConfig;

namespace Anay.TaskScheduling
{
    public partial class TaskConfig : Form
    {
        IConfigHelper configHelper = new ConfigFactory().GetInstance();
        private Ep_task_config config = null;
        private delegate void AppendStringCallback(string text);

        public TaskConfig()
        {
            InitializeComponent();
            RefreshConfigTree();
        }

        /// <summary>
        /// 初始化配置树
        /// </summary>
        public void RefreshConfigTree()
        {

            var configiList = configHelper.GetAllList();
            if (configiList.Count <= 0)
            {
                MessageBox.Show(@"没有找到配置文件'Task_config.json'");
                return;
            }
            configTree.Nodes.Clear();
            foreach (var item in configiList)
            {
                TreeNode node = new TreeNode()
                {
                    Text = string.Format("({0}){1}", item.Id, item.JobName),
                    Name = item.Id
                };
                if (item.IsEnabled == "0")
                {
                    node.ForeColor = Color.Green;//启用绿色
                }
                else
                {
                    node.ForeColor = Color.Red;//未启用红色
                }

                configTree.Nodes.Add(node);
            }
        }

        /// <summary>
        /// 配置树节点节点点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ConfigTree_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            var node = e.Node;
            if (node != null)
            {
                config = configHelper.GetTask_config(node.Name);
                if (config != null)
                {
                    Id.Text = config.Id;
                    JobName.Text = config.JobName;
                }
            }
        }

        /// <summary>
        /// 开始调度
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button4_Click(object sender, EventArgs e)
        {
            UploadJob job = new UploadJob
            {
                startTime = startTime.Value,
                endTime = endTime.Value,

            };

            if (config != null)
            {
                _ = job.Executeing(config, null, (s) =>
                {
                    if (this.LogBox.InvokeRequired)
                    {
                        this.LogBox.BeginInvoke(new AppendStringCallback(AppendString), new object[] { s });
                    }
                });
            }
            else
            {
                MessageBox.Show(@"未选择调度任务");
            }
        }

        public void AppendString(string text)
        {
            this.LogBox.Text += text;
        }

        public void ClearInput()
        {
            Id.Text = string.Empty;
            JobName.Text = string.Empty;
            LogBox.Text = string.Empty;
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            LogBox.Text = string.Empty;
        }
    }
}
